package com.grandtech.insurance.common;

import com.grandtech.insurance.domain.DbZzjg;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.apache.shiro.util.ByteSource;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Utils {
	private static final String SALT = "1qazxsw2";

	private static final String ALGORITH_NAME = "md5";

	private static final int HASH_ITERATIONS = 2;

	public static String encrypt(String idcode) {
		String newIdcode = new SimpleHash(ALGORITH_NAME, idcode, ByteSource.Util.bytes(SALT), HASH_ITERATIONS).toHex();
		return newIdcode;
	}

	public static String encrypt1(String idcode){
		String newIdcode = new SimpleHash(ALGORITH_NAME, idcode, null, HASH_ITERATIONS).toHex();
		return newIdcode;
	}

	public static String encrypt(String username, String pswd) {
		String newPassword = new SimpleHash(ALGORITH_NAME, pswd, ByteSource.Util.bytes(username + SALT),
				HASH_ITERATIONS).toHex();
		return newPassword;
	}

	public static String stringToMD5(String plainText) {
		byte[] secretBytes = null;
		try {
			secretBytes = MessageDigest.getInstance("md5").digest(
					plainText.getBytes());
		} catch (NoSuchAlgorithmException e) {
			throw new RuntimeException("没有这个md5算法！");
		}
		String md5code = new BigInteger(1, secretBytes).toString(16);
		for (int i = 0; i < 32 - md5code.length(); i++) {
			md5code = "0" + md5code;
		}
		return md5code;
	}

	public static void main(String[] args) {
		DbZzjg dbZzjg1 = new DbZzjg();
		if(dbZzjg1!=null){
			System.out.println(MD5Utils.stringToMD5("410000000000000000"));
			System.out.println(MD5Utils.encrypt("qq1", "111111"));
		}


	}

}
